package com.leospa.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;

import javax.swing.Timer;

import com.leospa.clients.SerialClient;
import com.leospa.properties.ConfigPropertiesFile;

public class MassageTimer {
	
	Timer timer;
	int time_in_sec = 0;
	int count = 0;
	Time previousTime = null;
	Time nextTime = null;
	Time timerStart = null;
	Time relayOnTime = null;
	Time relayOffTime = null;
	Time stopTimer = null;
	boolean realyOn = true;
	boolean realyOff = true;
	boolean timerClose = true;
	/*public MassageTimer(final int max_time, final String solarium_id, final int preTime, final int afterTime) {
		time_in_sec = max_time;
		count = time_in_sec;
		
		if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ System.out.println("Solarium ID Booked===Timer Start"+max_time);}
		timer = new Timer(1000, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ System.out.println(count+", Leo Spa "+solarium_id+"============"+getCurrentTime(""));}
				int starting_time = max_time - preTime;
					if(starting_time == count){
						if(ConfigPropertiesFile.DEBUG_MODE_ON == true){System.out.println("Leo Spa "+solarium_id+" Booked===Relay ON: "+getCurrentTime(""));}
						if(solarium_id.equals("1")){
							SerialClient.turnRelaySwitch1_On();
						} else if(solarium_id.equals("2")) {
							SerialClient.turnRelaySwitch2_On();
						} else if(solarium_id.equals("3")){
							SerialClient.turnRelaySwitch3_On();
						} else if(solarium_id.equals("4")){
							SerialClient.turnRelaySwitch4_On();
						}
					}
					if(afterTime ==  count){
						System.out.println("AfterTime: "+afterTime+", Count:"+count);
						if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ System.out.println("Leo Spa "+solarium_id+" Booked===Relay OFF: "+getCurrentTime(""));}
						if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ System.out.println(System.getProperty("java.library.path"));}
						if(solarium_id.equals("1")){
							SerialClient.turnRelaySwitch1_Off();
						} else if(solarium_id.equals("2")) {
							SerialClient.turnRelaySwitch2_Off();
						} else if(solarium_id.equals("3")){
							SerialClient.turnRelaySwitch3_Off();
						} else if(solarium_id.equals("4")){
							SerialClient.turnRelaySwitch4_Off();
						}
					}

				if(count == 0){	
					Hashtable massHash = Util.getUtilInstance().getMassageTimerHash();
					massHash.remove(solarium_id);
					timer.stop();
				}
				count--;				
			}
		});
		timer.start();
	}*/
	
	
	public MassageTimer(final int max_time, final String solarium_id, final int preTime, final int afterTime) {
		time_in_sec = max_time;
		count = time_in_sec-1;
		timerStart = new Time(addSecondToCurrentTime(0).getTime());
		nextTime = new Time(addSecondToCurrentTime(1).getTime());
		relayOnTime = new Time(addSecondToCurrentTime(preTime).getTime());
		relayOffTime = new Time(addSecondToCurrentTime(max_time-afterTime).getTime());
		stopTimer = new Time(addSecondToCurrentTime(max_time).getTime());
		
		if(ConfigPropertiesFile.DEBUG_MODE_ON == true){
			System.out.println("Timer Start: "+timerStart.toString());
			System.out.println("Relay On Time time: "+relayOnTime.toString());
			System.out.println("Relay Off Time time: "+relayOffTime.toString());
			System.out.println("Timer Stop: "+stopTimer.toString());
		}
		if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ System.out.println("Solarium ID Booked===Timer Start"+max_time);}
		timer = new Timer(100, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int starting_time = max_time - preTime;
					//System.out.println("Current Time: "+addSecondToCurrentTime(0)+", relayOnTime: "+relayOnTime+",  Compare: "+addSecondToCurrentTime(0).compareTo(relayOnTime));
					if((addSecondToCurrentTime(0).compareTo(relayOnTime) == 1 || addSecondToCurrentTime(0).compareTo(relayOnTime) == 0) && realyOn){
						realyOn = false;
						if(ConfigPropertiesFile.DEBUG_MODE_ON == true){System.out.println("Leo Spa "+solarium_id+" Booked===Relay ON: "+getCurrentTime(""));}
						if(solarium_id.equals("1")){
							SerialClient.turnRelaySwitch1_On();
						} else if(solarium_id.equals("2")) {
							SerialClient.turnRelaySwitch2_On();
						} else if(solarium_id.equals("3")){
							SerialClient.turnRelaySwitch3_On();
						} else if(solarium_id.equals("4")){
							SerialClient.turnRelaySwitch4_On();
						}
					}
					if((addSecondToCurrentTime(0).compareTo(relayOffTime) ==  1 || addSecondToCurrentTime(0).compareTo(relayOffTime) ==  0) && realyOff){
						realyOff = false;
						if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ System.out.println("Leo Spa "+solarium_id+" Booked===Relay OFF: "+getCurrentTime(""));}
						if(ConfigPropertiesFile.DEBUG_MODE_ON == true){ System.out.println(System.getProperty("java.library.path"));}
						if(solarium_id.equals("1")){
							SerialClient.turnRelaySwitch1_Off();
						} else if(solarium_id.equals("2")) {
							SerialClient.turnRelaySwitch2_Off();
						} else if(solarium_id.equals("3")){
							SerialClient.turnRelaySwitch3_Off();
						} else if(solarium_id.equals("4")){
							SerialClient.turnRelaySwitch4_Off();
						}
					}
					
				if(((addSecondToCurrentTime(0).compareTo(stopTimer) ==  1 || addSecondToCurrentTime(0).compareTo(stopTimer) ==  0) && timerClose) || max_time == 0){	
					timerClose = false;
					if(ConfigPropertiesFile.DEBUG_MODE_ON == true){System.out.println("Leo Spa "+solarium_id+" Timer Stop: "+getCurrentTime(""));}
					Hashtable massHash = Util.getUtilInstance().getMassageTimerHash();
					massHash.remove(solarium_id);
					timer.stop();
				}
				
				if(addSecondToCurrentTime(0).getSeconds()==nextTime.getSeconds()) {
					if(ConfigPropertiesFile.DEBUG_MODE_ON == true){System.out.println(solarium_id+" Timer: "+nextTime.toString());}
					nextTime = new Time(addSecondToCurrentTime(1).getTime());
					count--;
				}				
			}
		});
		timer.start();
	}
	
	public int getTime(){
		return count;
	}
	
	public static String getCurrentTime(String msg){
		Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(cal.getTime());
	}
	
	public static String getCurrentTimeTest(int addSecond){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        calendar.add(Calendar.SECOND, addSecond);
        return sdf.format(calendar.getTime());
	}
	
	public static Date addSecondToCurrentTime(int addSecond){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        calendar.add(Calendar.SECOND, addSecond);
        return calendar.getTime();//sdf.format(calendar.getTime())
	}
	
	public static Date getStringToDate(String actionTime) throws ParseException{
		 DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		 Date date = (Date)formatter.parse(actionTime);
		return date;
	}
}
